package com.magnificent.atlantis.client.ui.planner.panels.message;

import com.magnificent.atlantis.client.AtlantisConstants;
import com.magnificent.atlantis.client.data.configuration.ServerConfiguration;
import com.magnificent.atlantis.client.data.message.messages.ClientMessage;
import com.magnificent.atlantis.client.data.message.ClientMessageHelper;
import com.magnificent.atlantis.client.data.settings.User;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;

import javax.swing.*;
import java.awt.*;

/**
 * User: Babanin
 * Date: 23.04.2009
 * Time: 4:43:02
 */
public class MessageTableRenderer extends DefaultTableRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel component = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);    //To change body of overridden methods use File | Settings | File Templates.

        component.setText("");
        component.setIcon(null);
        component.setToolTipText("");

        MessageShell messageShell = (MessageShell) value;
        ClientMessage clientMessage = messageShell.getMessage();
        java.util.List<ServerConfiguration> serverConfigurations = messageShell.getContainer().getServerConfigurations();

        ServerConfiguration configuration = null;
        for (ServerConfiguration serverConfiguration : serverConfigurations) {
            if (serverConfiguration.getId() == clientMessage.getConfigurationId()) {
                configuration = serverConfiguration;
            }
        }

        StringBuilder builder = new StringBuilder();

        switch (column) {
            // Recipient / Sender
            case 0:
                switch (clientMessage.getMessageType()) {
                    case INBOX:
                        component.setText(ClientMessageHelper.senderToString(clientMessage, messageShell.getContainer()));
                        break;
                    case OUTBOX:
                        builder = new StringBuilder();

                        java.util.List<User> outboxUserList = configuration.getUsers(clientMessage.getUsersToIds());
                        for (User user : outboxUserList) {
                            builder.append(user.getSurname()).append(" ").append(user.getName().toCharArray()[0]).append("., ");
                        }

                        String userString = builder.toString().substring(0, builder.length() - 2);

                        component.setText(userString);
                        component.setToolTipText(userString);
                        break;
                    case SENT:
                        builder = new StringBuilder();

                        for (User user : configuration.getUsers(clientMessage.getUsersToIds())) {
                            builder.append(user.getSurname()).append(" ").append(user.getName().toCharArray()[0]).append("., ");
                        }

                        String userToString = builder.toString().substring(0, builder.length() - 2);

                        component.setText(userToString);
                        component.setToolTipText(userToString);
                        break;
                }
                break;
            case 1:
                component.setText(clientMessage.getTitle());
                break;
            case 2:
                component.setText(AtlantisConstants.DATE_TIME_FORMAT.format(clientMessage.getSentDate()));
                break;
            case 3:
                component.setText(configuration.getName());
        }

        return component;
    }
}
